/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo3;

import Modelo.DAL.Extintor_DAO;
import Acceso.Security;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author glpc
 */
public class consultarExtintorAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private static final String[] ALLOWED = {"Administrador","Gestionar Sistemas de Extincion"};

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        if (!Security.validateSession(request, ALLOWED)) {
            Security.privilegeError(response);
            return mapping.findForward("errorPriv");
        }

        String id_piso = request.getParameter("id_piso");
        String sede = request.getParameter("sede");
        String nombre_edificio = request.getParameter("nombre_edificio");
        String tipo = request.getParameter("tipo");
        String capacidad = request.getParameter("capacidad");
        String fechadesde = request.getParameter("fechadesde");
        String fechahasta = request.getParameter("fechahasta");
        String status = request.getParameter("status");
        String personaContacto = request.getParameter("personaContacto");
        String senalizado = request.getParameter("senalizado");
        String usado = request.getParameter("usado");
        String categoria = request.getParameter("categoria");
        String referencia = request.getParameter("referencia");
        String order = request.getParameter("order");
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");


        String atributos = " * ";
        String from = " from extintor ";
        String select = "Select ";
        String selectCount = "Select count(id_nro_bien) ";
        String where = " where 1 = 1 ";
        String limite = "";


        if (id_piso != null && !id_piso.equals("")) {
            where += " and id_piso = " + Integer.parseInt(id_piso);
        }

        if (sede != null && !sede.equals("")) {
            where += " and nombre_sede = '" + sede + "'";
        }
        if (categoria != null && !categoria.equals("")) {
            where += " and categoria = '" + categoria + "'";
        }

        if (nombre_edificio != null && !nombre_edificio.equals("")) {
            where += " and nombre_edificio = '" + nombre_edificio + "'";
        }

        if (tipo != null && !tipo.equals("")) {
            where += " and lower(tipo) like  lower('%" + tipo + "%')";
        }

        if (capacidad != null && !capacidad.equals("")) {
            where += " and lower(capacidad) like lower('%" + capacidad + "'%)";
        }

        if (status != null && !status.equals("")) {
            where += " and status = '" + status + "'";
        }

        if (personaContacto != null && !personaContacto.equals("")) {
            where += " and personaContacto = '" + personaContacto + "'";
        }

        if (senalizado != null && !senalizado.equals("")) {
            where += " and senalizado = '" + senalizado + "'";
        }

        if (usado != null && !usado.equals("")) {
            where += " and usado = '" + usado + "'";
        }

        if (referencia != null && !referencia.equals("")) {
            where += " and referencia = '" + referencia + "'";
        }


        if (fechadesde != null && !fechadesde.equals("")) {
            where += " and fecha_ultima_recarga >= to_date('" + fechadesde + "','dd/mm/yyyy')";
        }

        if (fechahasta != null && !fechahasta.equals("")) {
            where += " and fecha_ultima_recarga <= to_date('" + fechahasta + "','dd/mm/yyyy')";
        }

        if (limit != null && !limit.equals("0")) {
            limite += " limit " + limit + " offset " + offset;
        }
        if (order != null && !order.equals("")) {
            select += " ( " + selectCount + from + where + " ) , " + atributos + from + where + order + limite + ";";
        } else {
            select += " ( " + selectCount + from + where + " ) , " + atributos + from + where + limite + ";";
        }


        response.setContentType("text/json");
        JSONArray extintor = Extintor_DAO.consultarExtintor(select);
        if (extintor == null) {
            return mapping.findForward(FAILURE);
        }
        JSONObject ext = new JSONObject();
        ext.put("status", "ok");
        ext.put("consultarExtintor", extintor);
        PrintWriter pw = response.getWriter();
        pw.write(ext.toString());
        pw.flush();
        pw.close();

        return mapping.findForward(SUCCESS);

    }
}
